/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.tothgya.timetable.volan;

import hu.tothgya.timetable.Journey;
import hu.tothgya.timetable.Timetable;
import hu.tothgya.timetable.utils.NonUTF8WMLConverter;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author tothgya
 */
public class TimetableFactory extends hu.tothgya.timetable.TimetableFactory {

    private String volanUID = null;
    private static final String urlstr = "http://wap.menetrendek.hu/cgi-bin/menetrend/wml.cgi";

    private enum Direction {

        START {

            @Override
            public String toString() {
                return "honnan";
            }
        },
        STOP {

            @Override
            public String toString() {
                return "hova";
            }
        }
    }

    /**
     * Sets the uid for the session.
     */
    private void setVolanUID() {
        {
            Document d = NonUTF8WMLConverter.createDocument(urlstr);

            NodeList nodelist = d.getElementsByTagName("postfield");

            Logger.getLogger(TimetableFactory.class.getName()).log(Level.INFO, d.getTextContent());

            for (int i = 0; i < nodelist.getLength(); i++) {
                Node n = nodelist.item(i);
                String attrName = n.getAttributes().getNamedItem("name").getNodeValue();
                if (attrName.equals("uid")) {
                    volanUID = n.getAttributes().getNamedItem("value").getNodeValue();
                    break;
                }
            }

        }
    }

    private void setStop(String stopName, Direction direction) {
        {
            String data = "action=valaszt&show_from=0&mikor=0&napszak=0&rendezes=0&irany=" + direction + "&uid=" + volanUID;
            Document d = NonUTF8WMLConverter.createDocument(urlstr, data);
        }
        {
            String data = "action=match&beirta=" + stopName + "&uid=" + volanUID;
            Document d = NonUTF8WMLConverter.createDocument(urlstr, data);
        }
    }

    public TimetableFactory(String fromString, String toString, Calendar date) {

        setVolanUID();

        setStop(fromString, Direction.START);
        setStop(toString, Direction.STOP);

        String data = "action=keres&show_from=0&mikor=0&napszak=0&rendezes=0&uid=" + volanUID;
/*
        try {
            URL url = new URL(urlstr);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();



        } catch (MalformedURLException ex) {
            Logger.getLogger(TimetableFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TimetableFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
*/


    }

    @Override
    public List<Journey> getJourneys() {
        throw new UnsupportedOperationException("Not supported yet.");


    }

    @Override
    public Timetable getJourneyTimetable() {
        throw new UnsupportedOperationException("Not supported yet.");


    }

    @Override
    public Timetable getConnectionTimeTable() {
        throw new UnsupportedOperationException("Not supported yet.");

    }
}
